# Contributor: John Coyle <dx9err@gmail.com>
# Contributor: Iggy Jackson <iggy@kws1.com>
pkgname=ceph
pkgver=14.2.9
pkgrel=1
pkgdesc="Ceph is a distributed object store and file system"
pkgusers="ceph"
pkggroups="ceph"
url="https://ceph.com/"
arch="x86_64 aarch64"
# https://github.com/ceph/ceph/blob/master/COPYING
license="LGPL-2.1-only AND LGPL-2.0-or-later AND GPL-2.0-only AND GPL-3.0-only AND CC-BY-SA-1.0 AND BSL-1.0 AND GPL-2.0-or-later WITH Autoconf-exception-2.0 AND BSD-3-Clause AND MIT AND custom"
depends="ceph-osd ceph-mds ceph-mon"
_base_deps="
	cryptsetup
	e2fsprogs
	logrotate
	parted
	util-linux
	xfsprogs
	"
_osd_daemon_deps="fuse snappy lz4-libs"
_osd_tools_deps="lz4-libs"
_ceph_volume_deps="lvm2"
_ceph_test_deps="xmlstarlet"
makedepends="
	acl-dev
	argp-standalone
	bc
	boost-dev
	btrfs-progs
	bzip2-dev
	cmake
	coreutils
	cunit-dev
	curl-dev
	cython
	diffutils
	eudev-dev
	expat-dev
	fcgi-dev
	flex
	fuse
	fuse-dev
	git
	grep
	gperf
	jq
	keyutils-dev
	leveldb-dev
	libaio-dev
	libatomic_ops-dev
	libcap-ng-dev
	libedit-dev
	openssl-dev
	libnl3-dev
	libtirpc-dev
	libtool
	libxml2-dev
	linux-headers
	lvm2-dev
	lz4-dev
	nodejs
	nss-dev
	oath-toolkit-dev
	openldap-dev
	procps
	python3-dev
	py3-pip
	py3-nose
	py3-setuptools
	py3-sphinx
	py3-virtualenv
	rabbitmq-c-dev
	readline-dev
	rpcgen
	snappy-dev
	userspace-rcu-dev
	xfsprogs-dev
	xmlstarlet
	yasm
	$_base_deps
	$_osd_daemon_deps
	$_osd_tools_deps
	$_ceph_volume_deps
	$_ceph_test_deps
"

source="https://download.ceph.com/tarballs/ceph_$pkgver.orig.tar.gz
	allperms.patch
	musl-fixes.patch
	fix-seek-data-hole.patch
	"
subpackages="
	$pkgname-doc
	$pkgname-base
	$pkgname-common
	$pkgname-mds
	$pkgname-mgr
	$pkgname-mon
	$pkgname-mon-daemon:mon_daemon
	$pkgname-mon-tools:mon_tools
	$pkgname-fuse:ceph_fuse
	$pkgname-openrc
	$pkgname-osd
	$pkgname-osd-daemon:osd_daemon
	$pkgname-osd-tools:osd_tools
	$pkgname-volume:ceph_volume:noarch
	$pkgname-radosgw
	$pkgname-bash-completion:bash_completion:noarch
	$pkgname-dev
	$pkgname-user
	rbd-fuse:rbd_fuse
	rbd-mirror:rbd_mirror
	rbd-nbd:rbd_nbd
	librbd
	libcephfs
	librados
	py3-rados:_py3_rados
	libradosstriper
	py3-rbd:_py3_rbd
	py3-cephfs:_py3_cephfs
"

# secfixes:
#   14.2.9-r0:
#     - CVE-2020-1759
#     - CVE-2020-1760
#   14.2.7-r0:
#     - CVE-2020-1699
#     - CVE-2020-1700
#   14.2.3-r0:
#     - CVE-2019-10222

_ceph_uid=167
_ceph_gid=167

_prefix=/usr
_bindir=$_prefix/bin
_datadir=$_prefix/share
_docdir=$_datadir/doc
_libdir=$_prefix/lib
_libexecdir=$_prefix/libexec
_localstatedir=/var
_mandir=$_datadir/man
_sbindir=/usr/sbin
_syssbindir=/sbin
_sysconfdir=/etc

_udevrulesdir=/etc/udev/rules.d

_py3_sitelib() {
	python3 -c "import site; print(site.getsitepackages()[0])"
}

build() {
	export CEPH_BUILD_VIRTUALENV="$builddir"

	# builders keep failing when -jN == nproc
	export MAKEFLAGS="$MAKEFLAGS -j$((JOBS<16 ? JOBS : 16))"

	mkdir -p "$builddir"/build
	cd "$builddir"/build

	cmake .. \
		-DALLOCATOR=libc \
		-DCMAKE_INSTALL_PREFIX=$_prefix \
		-DCMAKE_INSTALL_LIBDIR=$_libdir \
		-DCMAKE_INSTALL_LIBEXECDIR=$_libexecdir \
		-DCMAKE_INSTALL_LOCALSTATEDIR=$_localstatedir \
		-DCMAKE_INSTALL_SYSCONFDIR=$_sysconfdir \
		-DCMAKE_INSTALL_DOCDIR=$_docdir/ceph \
		-DCMAKE_INSTALL_MANDIR=$_mandir \
		-DWITH_REENTRANT_STRSIGNAL=ON \
		-DWITH_THREAD_SAFE_RES_QUERY=ON \
		-DWITH_MANPAGE=ON \
		-DWITH_SYSTEM_BOOST=ON \
		-DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 \
		-DWITH_PYTHON2=OFF \
		-DWITH_PYTHON3=ON \
		-DMGR_PYTHON_VERSION=3 \
		-DWITH_LTTNG=OFF \
		-DWITH_RDMA=OFF \
		-DWITH_SYSTEMD=OFF \
		-DWITH_SPDK=OFF \
		-DWITH_BABELTRACE=OFF \
		-DWITH_RADOSGW_AMQP_ENDPOINT=OFF \
		-DWITH_TESTS=OFF
	make

}

package() {
	cd "$builddir"/build
	make DESTDIR="$pkgdir" install
	cd ..

	rm -f "$pkgdir"$_sysconfdir/init.d/ceph

	# Move mount.* binaries to /sbin
	mkdir -p "$pkgdir"$_syssbindir
	mv "$pkgdir"$_sbindir/mount.* "$pkgdir"$_syssbindir

	# We need to clean this out before packaging as it's freaking huge
	rm -rf "$pkgdir"$_datadir/ceph/mgr/dashboard/frontend/node_modules

	install -m 0644 -D src/etc-rbdmap "$pkgdir"$_sysconfdir/ceph/rbdmap
	install -m 0644 -D src/logrotate.conf "$pkgdir"$_sysconfdir/logrotate.d/ceph
	install -m 0644 -D etc/sysctl/90-ceph-osd.conf "$pkgdir"/$_sysconfdir/sysctl.d/90-ceph-osd.conf
	chmod 0644 "$pkgdir"$_docdir/ceph/sample.ceph.conf

	# udev rules
	install -m 0644 -D udev/50-rbd.rules "$pkgdir"$_udevrulesdir/50-rbd.rules

	# sudoers.d
	install -m 0600 -D sudoers.d/ceph-osd-smartctl "$pkgdir"/$_sysconfdir/sudoers.d/ceph-osd-smartctl
}

check() {
	cd "$builddir"/build
	ctest
}

# This subpackage is a little weird and a side effect of trying to keep
# the daemon packages as small as possible. We might be able to roll this into
# the base/common package later if we can slim those down or break them up in a
# different way
user() {
	pkgdesc="Minimal package to create a user for other ceph packages."
	depends=""
	install="$pkgname-user.pre-install"
	mkdir -p "$subpkgdir"
}

base() {
	pkgdesc="Base is the package that includes all the files shared amongst ceph servers"
	depends="$_base_deps
		$pkgname-user=$pkgver-r$pkgrel
		$pkgname-common=$pkgver-r$pkgrel
		librbd=$pkgver-r$pkgrel
		librados=$pkgver-r$pkgrel
		libcephfs=$pkgver-r$pkgrel
	"

	_pkg $_bindir \
		ceph-crash \
		crushtool \
		monmaptool \
		osdmaptool \
		ceph-kvstore-tool \
		ceph-run
	_pkg $_sbindir ceph-create-keys
	_pkg $_libexecdir/ceph ceph_common.sh
	_pkg $_libdir/rados-classes '*.so*'
	_pkg $_libdir/ceph/erasure-code 'libec_*.so*'
	_pkg $_libdir/ceph/compressor 'libceph_*.so*'
	_pkg $_libdir/ceph/crypto 'libceph_*.so*'
	_pkg $_sysconfdir/logrotate.d ceph
	for dir in crash crash/posted tmp bootstrap-osd bootstrap-mds \
			bootstrap-rgw bootstrap-mgr bootstrap-rbd \
			bootstrap-rbd-mirror; do

		install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
			"$subpkgdir"$_localstatedir/lib/ceph/$dir
	done
}

common() {
	pkgdesc="Common utilities to mount and interact with a ceph storage cluster."
	depends="py3-rados py3-rbd py3-cephfs"

	_pkg $_bindir ceph \
		ceph-authtool \
		ceph-conf \
		ceph-dencoder \
		ceph-rbdnamer \
		ceph-syn \
		cephfs-data-scan \
		cephfs-journal-tool \
		cephfs-table-tool \
		rados \
		rbd \
		rbd-replay \
		rbd-replay-many \
		rbdmap \
		ceph-post-file
	_pkg $_syssbindir mount.ceph
	_pkg $_datadir/ceph known_hosts_drop.ceph.com \
		id_rsa_drop.ceph.com \
		id_rsa_drop.ceph.com.pub
	_pkg $_sysconfdir/ceph rbdmap

	_pkg "$(_py3_sitelib)" ceph_argparse.py* ceph_daemon.py*

	_pkg $_udevrulesdir 50-rbd.rules
	install -m 3770 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/log/ceph
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph
}

mds() {
	pkgdesc="Metadata server daemon for the Ceph distributed file system."
	depends="ceph-base"

	_pkg $_bindir ceph-mds
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/mds
}

mon() {
	pkgdesc="Cluster monitor daemon virtual package for the Ceph distributed file system."
	depends="$pkgname-base
		$pkgname-mon-daemon=$pkgver-r$pkgrel
		$pkgname-mon-tools=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
}

mon_daemon() {
	pkgdesc="Cluster monitor daemon for the Ceph distributed file system."
	depends="$pkgname-user=$pkgver-r$pkgrel"

	_pkg $_bindir ceph-mon
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/mon
}

mon_tools() {
	pkgdesc="Cluster monitor tools for the Ceph distributed file system."
	depends=

	_pkg $_bindir ceph-monstore-tool
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/mon
}

ceph_fuse() {
	pkgdesc="FUSE based client for Ceph distributed network file system."
	depends=
	_pkg $_bindir ceph-fuse
	_pkg $_syssbindir mount.fuse.ceph
}

rbd_fuse() {
	pkgdesc="FUSE based client to map Ceph rbd images to files."
	depends=
	_pkg $_bindir rbd-fuse
}

rbd_mirror() {
	pkgdesc="Daemon for mirroring RBD images between Ceph clusters."
	depends="ceph-common=$pkgver-r$pkgrel"
	_pkg $_bindir rbd-mirror
}

rbd_nbd() {
	pkgdesc="NBD based client to map Ceph rbd images to local device."
	depends=
	_pkg $_bindir rbd-nbd
}

radosgw() {
	pkgdesc="Rados REST gateway which implements Amazon's S3 and OpenStack's Swift APIs."
	depends="ceph-common oath-toolkit-liboath"
	_pkg $_bindir radosgw \
		radosgw-admin \
		radosgw-es \
		radosgw-object-expirer \
		radosgw-token
	mkdir -p "$subpkgdir"$_localstatedir/lib/ceph/radosgw
}

osd() {
	pkgdesc="Object storage daemon virtual package for the Ceph distributed file system."
	depends="$pkgname-osd-daemon=$pkgver-r$pkgrel
		$pkgname-osd-tools=$pkgver-r$pkgrel
		$pkgname-volume=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
}

osd_daemon() {
	pkgdesc="Object storage daemon for the Ceph distributed file system."
	depends="$_osd_daemon_deps $pkgname-user=$pkgver-r$pkgrel"

	_pkg $_bindir ceph-osd
	_pkg $_libexecdir/ceph ceph-osd-prestart.sh
	_pkg $_sysconfdir/sudoers.d ceph-osd-smartctl
	_pkg $_sysconfdir/sysctl.d 90-ceph-osd.conf
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/osd
}

osd_tools() {
	pkgdesc="Object storage daemon tools for the Ceph distributed file system."
	depends="$pkgname-base=$pkgver-r$pkgrel $_osd_tools_deps"

	_pkg $_bindir ceph-clsinfo \
		ceph-bluestore-tool \
		ceph-objectstore-tool \
		ceph-osdomap-tool
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/osd
}

ceph_volume() {
	pkgdesc="ceph-volume tool for the Ceph distributed file system."
	depends="$_ceph_volume_deps"

	_pkg $_sbindir ceph-volume
	_pkg "$(_py3_sitelib)" ceph_volume 'ceph_volume-*'
}

librados() {
	pkgdesc="RADOS distributed object store client library"
	depends=
	_pkg $_libdir librados.so.*
	_pkg $_libdir/ceph libceph-common.so.*
}

_py3_rados() {
	pkgdesc="Python libraries for the RADOS object store"
	depends=

	_pkg "$(_py3_sitelib)" rados*.so rados-*.egg-info
}

libradosstriper() {
	pkgdesc="RADOS striping library"
	depends=

	_pkg $_libdir libradosstriper.so.*
}

librbd() {
	pkgdesc="RADOS block device client library"
	depends=
	_pkg $_libdir librbd.so.*
}

_py3_rbd() {
	pkgdesc="Python libraries for the RADOS block device"
	depends="py3-rados"

	_pkg "$(_py3_sitelib)" rbd*.so rbd-*.egg-info
}

libcephfs() {
	pkgdesc="Ceph distributed file system client library"
	depends=
	_pkg $_libdir libcephfs.so.*
}

_py3_cephfs() {
	pkgdesc="Python libraries for Ceph distributed file system"
	depends="py3-rados"

	_pkg "$(_py3_sitelib)" cephfs*.so cephfs-*.egg-info ceph_volume_client.py*
}

ceph_test() {
	pkgdesc="Ceph benchmarks and test tools"
	depends="ceph-common=$pkgver-r$pkgrel $_ceph_test_deps"

	_pkg $_bindir ceph-client-debug \
		ceph_bench_log \
		ceph_kvstorebench \
		ceph_multi_stress_watch \
		ceph_erasure_code \
		ceph_erasure_code_benchmark \
		ceph_omapbench \
		ceph_objectstore_bench \
		ceph_perf_objectstore \
		ceph_perf_local \
		ceph_perf_msgr_client \
		ceph_perf_msgr_server \
		ceph_psim \
		ceph_radosacl \
		ceph_rgw_jsonparser \
		ceph_rgw_multiparser \
		ceph_scratchtool \
		ceph_scratchtoolpp \
		ceph_smalliobench \
		ceph_smalliobenchdumb \
		ceph_smalliobenchfs \
		ceph_smalliobenchrbd \
		ceph_test_* \
		ceph_tpbench \
		ceph_xattr_bench \
		ceph-coverage \
		ceph-monstore-tool \
		ceph-osdomap-tool \
		ceph-kvstore-tool \
		ceph-debugpack

	_pkg $_libdir ceph/ceph-monstore-update-crush.sh
}

bash_completion() {
	pkgdesc="Bash completions for Ceph"
	depends=
	install_if="ceph=$pkgver-r$pkgrel bash-completion"
	_pkg $_sysconfdir/bash_completion.d '*'
}

mgr() {
	pkgdesc="Ceph Manager Daemon"
	depends="ceph-base=$pkgver-r$pkgrel"

	_pkg $_bindir ceph-mgr
	_pkg $_datadir/ceph mgr

	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"$_localstatedir/lib/ceph/mgr
}

_pkg() {
	local path=$1
	shift
	if ! [ -d "$pkgdir"/$path ]; then
		error "$pkgdir/$path is not a directory"
		return 1
	fi
	mkdir -p "$subpkgdir"$path
	for _file in "$@"; do
		mv "$pkgdir"$path/$_file "$subpkgdir"$path
	done
}

sha512sums="0a26372d0037c32fe1d24be880e272fcce5089b02adfb0e791e0406155f6e66c913fa43762028f49c1fa3f30c6c8d819c521dd11702a37cc7040dfd3097c068e  ceph_14.2.9.orig.tar.gz
e1becd813ed3f28e2e4a6bef78b3b5117c1c0bb9cabe0ba9c912e0a20b551b6b2667495cddb94acd64192e287144911ff1c11e0d636fe04cc458146cfb0daca8  allperms.patch
35722b11ad52a3145153635b6a96abda2a23ae9c7e63e2eac006c1e5b8014452c4a1a11bbe0292fd731e4c43aa38e27dd75d2ff9d25bcf52290278f71e868570  musl-fixes.patch
951eab175ffcfc39675302ea9ea5ac18c9c348a0c5180c6b1f376e4547c079dd660ec50e08e32d47a4144bd17248b81842df85e55a93903054f58f976d85f8a1  fix-seek-data-hole.patch"
